function isOperator(val)
{
	return ((val == '+') || (val == '-') || (val == 'x') || (val == '/') || (val == 'sin') || (val == 'cos') || (val == 'tan') || (val == 'cot') || (val == 'log') || (val == 'ln') || (val == '^') || (val == 'v') || (val == 'inv') || (val == 'sqr') || (val == 'sqrt'));
}

function isOperand(val)
{
	return (!isNaN(val - 0));
}

function priority(operator)
{
	if (operator == '+' || operator == '-')
	{
		return 1;
	}
	if (operator == 'x' || operator == '/')
	{
		return 2;
	}
	if (operator == '^')
	{
		return 3;
	}
	if (operator == 'sin' || operator == 'cos' || operator == 'tan' || operator == 'cot' || operator == 'log' || operator == 'ln' || operator == 'v' || operator == 'inv' || (operator == 'sqr') || (operator == 'sqrt'))
	{
		return 4;
	}
}

function conv2tokenArray(ex)
{
	var pattern = /\s{1,}/g; 
	ex = ex.replace(pattern, ' ');
	return ex.split(' ');
}

function infix2postfix(ex)
{
	var postfixArr = [];
	var tokenArray = conv2tokenArray(ex);
	var stack = new Stack();
	for (var i = 0; i < tokenArray.length; i++)
	{
		if (tokenArray[i] == '')
		{
			continue;
		}
		if (tokenArray[i] == '(')
		{
			stack.push('(');
		}
		else if (tokenArray[i] == ')')
		{
			var x = stack.pop();
			while (x != '(')
			{
				postfixArr[postfixArr.length] = x;
				x = stack.pop();
			}
		}
		else if (isOperand(tokenArray[i]))
		{
			postfixArr[postfixArr.length] = tokenArray[i];
		}
		else if (isOperator(tokenArray[i]))
		{
			while (!stack.isEmpty() && priority(stack.peek()) >= priority(tokenArray[i]))
			{
				postfixArr[postfixArr.length] = stack.pop();
			}
			stack.push(tokenArray[i]);
		}
	}
	while (!stack.isEmpty())
	{
		postfixArr[postfixArr.length] = stack.pop();
	}
	return postfixArr;
}